package com.lungle.warmdiary.app.view.testcampaign.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.response.Campaign;
import com.lungle.warmdiary.app.utils.StringUtils;
import com.lungle.warmdiary.app.utils.TDevice;
import com.lungle.warmdiary.app.utils.UIHelper;
import com.lungle.warmdiary.app.view.testcampaign.CampaignListTab;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhangxueyuan on 16/9/28.
 * <p>
 * 亲测列表 ceil adapter
 */
public class CampaignAdapter extends ListBaseAdapter {

    ImageLoader imageLoader;

    public CampaignAdapter(Context context) {
        super(context);
    }

    @Override
    protected View getRealView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        imageLoader = ImageLoader.getInstance();
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(
                    R.layout.list_cell_campaign, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        final Campaign item = (Campaign) mDatas.get(position);

        imageLoader.displayImage(item.getIcon(), vh.campaignIcon);
        final String title = String.format("%s | %s", item.getName(), item.getBrief());
        vh.campaignTitle.setText(title);
        vh.campaignVendor.setText(item.getVendor());
        if (item.getRate() == 0) {
            vh.campaignRate.setVisibility(View.GONE);
            vh.campaignNum.setVisibility(View.GONE);
        } else {
            vh.campaignRate.setVisibility(View.VISIBLE);
            vh.campaignNum.setVisibility(View.VISIBLE);
        }

        vh.campaignRate.setRating(item.getRate());
        vh.campaignNum.setText(String.format("(%d)", item.getNtest()));

        List<String> images = item.getImages();
        if (images.size() >= 2) {

            vh.campaignImg1.setVisibility(View.VISIBLE);
            vh.campaignImg2.setVisibility(View.VISIBLE);

            int imageWidth = getImageWidth();
            int imageHeigth = (int) (mContext.getResources().getDimension(R.dimen.campaign_screen_h) / mContext.getResources().getDimension(R.dimen.campaign_screen_w) * imageWidth);

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) vh.campaignImg1.getLayoutParams();
            params.height = imageHeigth;
            params.width = imageWidth;
            vh.campaignImg1.setLayoutParams(params);

            LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) vh.campaignImg2.getLayoutParams();
            params1.height = imageHeigth;
            params1.width = imageWidth;
            vh.campaignImg2.setLayoutParams(params1);

            DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
                    .imageScaleType(ImageScaleType.EXACTLY)
                    .showImageOnLoading(R.drawable.big_imgplaceholder)
                    .showImageForEmptyUri(R.drawable.big_imgplaceholder)
                    .bitmapConfig(Bitmap.Config.RGB_565).build();
            imageLoader.displayImage(StringUtils.getPreviewImageUrl(images.get(0)), vh.campaignImg1, displayOptions);
            imageLoader.displayImage(StringUtils.getPreviewImageUrl(images.get(1)), vh.campaignImg2, displayOptions);
        } else if (images.size() == 1) {

            vh.campaignImg2.setVisibility(View.GONE);
            vh.campaignImg1.setVisibility(View.VISIBLE);

            int imageWidth = getImageWidth();
            int imageHeigth = (int) (mContext.getResources().getDimension(R.dimen.campaign_screen_h) / mContext.getResources().getDimension(R.dimen.campaign_screen_w) * imageWidth);

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) vh.campaignImg1.getLayoutParams();
            params.height = imageHeigth;
            params.width = imageWidth;
            vh.campaignImg1.setLayoutParams(params);


            DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
                    .imageScaleType(ImageScaleType.EXACTLY)
                    .showImageOnLoading(R.drawable.big_imgplaceholder)
                    .showImageForEmptyUri(R.drawable.big_imgplaceholder)
                    .bitmapConfig(Bitmap.Config.RGB_565).build();
            imageLoader.displayImage(StringUtils.getPreviewImageUrl(images.get(0)), vh.campaignImg1, displayOptions);

        } else {
            vh.campaignImg1.setVisibility(View.GONE);
            vh.campaignImg2.setVisibility(View.GONE);
        }

        vh.campaignReward.setText(String.format("%s%.2f 元", "亲测奖金：", item.getPrize()));
        vh.campaignStatus.setText(CampaignListTab.getTabByStatus(item.getStatus()).getTitle());
        vh.campaignStatus.setTextColor(CampaignListTab.getTabByStatus(item.getStatus()).color);

//        String startTime = item.getStarttime().split(" ")[0];


        String endTime = item.getEndtime();
        if (TextUtils.isEmpty(endTime)) {
            vh.campaign_time.setText("亲测时间：长期有效");
        } else {
            endTime = item.getEndtime().split(" ")[0];
            vh.campaign_time.setText(String.format("亲测结束时间：%s", endTime));
        }
        
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoDetail(item, title);
            }
        });


        return convertView;
    }


    private int getImageWidth() {
        return (int) ((TDevice.getScreenWidth() - 3 * TDevice.dpToPixel(10)) / 2);
    }

    private void gotoDetail(final Campaign item, String title) {
        UIHelper.showCampaignDetail(mContext, item.getId(), item.getIcon(), title, item.getRate());
    }


    static class ViewHolder {

        @Bind(R.id.campaign_icon)
        ImageView campaignIcon;
        @Bind(R.id.campaign_title)
        TextView campaignTitle;
        @Bind(R.id.campaign_vendor)
        TextView campaignVendor;
        @Bind(R.id.campaign_num)
        TextView campaignNum;
        @Bind(R.id.campaign_rate)
        RatingBar campaignRate;
        @Bind(R.id.campaign_img1)
        ImageView campaignImg1;
        @Bind(R.id.campaign_img2)
        ImageView campaignImg2;
        @Bind(R.id.campaign_reward)
        TextView campaignReward;
        @Bind(R.id.campaign_status)
        TextView campaignStatus;

        @Bind(R.id.campaign_time)
        TextView campaign_time;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
